
<%@ page import="fi.hell.lunchit.User" %>
<%@ page import="fi.hell.lunchit.Round" %>
<%@ page import="fi.hell.lunchit.Restaurant" %>

<g:set var="currentRound" value="${Round.currentRound()}" />
<g:set var="currentVote" value="${Round.currentVote(currentRound)}" />
<g:set var="loggedInUser" value="${User.getLoggedInUser()}" />
<g:set var="locale" value="${grailsApplication.config.locale}" />

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <title>${userInstance} - LunchIT</title>
    </head>
    <body>
        <div class="nav">
            <span class="menuButton"><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></span>
        </div>
        
        <g:render template="/panels" model="[loggedInUser:loggedInUser, currentRound:currentRound, currentVote:currentVote]" />
        
        <div id="pageBody">

            <h1>${userInstance}</h1>
            <g:if test="${flash.message}">
            	<div class="message">${flash.message}</div>
            </g:if>

            <div class="userStatisticsBox">
              <h2><g:message code="userStatistics" default="User statistics" /></h2>

              <div class="statBox">
                  <h3><g:message code="favouriteSelections" default="Favourite selections" /></h3>
                  <ul class="roundResults">
                      <g:each var="r" in="${userInstance.favouritRestaurants()}">
                          <li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="times" default="times" /></li>
                      </g:each>
                  </ul>
              </div>

              <div class="statBox">
                  <h3><g:message code="mostVotedRestaurants" default="Most voted restaurants" /></h3>
                  <ul class="roundResults">
                      <g:each var="r" in="${userInstance.mostVotedRestaurants()}">
                          <li class="controller"><g:link controller="restaurant" action="show" id="${r.key.id}">${r.key}</g:link> - ${r.value} <g:message code="points" default="points" /></li>
                      </g:each>
                  </ul>
              </div>

              <div class="statBox">
                  <h3><g:message code="totalMoneyUsed" default="Total money used" /></h3>
                  <g:set var="totalMoneyUsed" value="${userInstance.moneyUsed()/100}" />
                  <p><g:formatNumber number="${totalMoneyUsed}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
              </div>

              <div class="statBox">
                  <h3><g:message code="avgCostPerLunch" default="Average cost per lunch" /></h3>
                  <g:if test="${Round.countByUserAndSelectionIsNotNull(userInstance) > 0}">
                    <p><g:formatNumber number="${totalMoneyUsed/Round.countByUserAndSelectionIsNotNull(userInstance)}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
                  </g:if>
                  <g:else>
                    <p><g:formatNumber number="${0}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
                  </g:else>
              </div>
            </div>

            <div class="list">
                <table>
                    <thead>
                        <tr>
                            <g:sortableColumn property="day" title="${message(code: 'round.day.label', default: 'Day')}" />
                        
                            <g:sortableColumn property="selection" title="${message(code: 'round.selection.label', default: 'Selection')}" />
                            
                            <th>${message(code: 'round.votes', default: 'Votes')}</th>
                        
                            <g:sortableColumn property="price" title="${message(code: 'round.price.label', default: 'Price')}" />
                        
                            <th>${message(code: 'round.lunchMates', default: 'Lunch mates')}</th>
                        </tr>
                    </thead>
                    <tbody>
                    <g:each in="${rounds}" status="i" var="round">
                        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
                            <g:if test="${!round.selection || !round.price}">
                              <form action="${createLink(controller:'round', action:'update')}" method="post">
                                <input type="hidden" name="id" value="${round.id}" />
                                <input type="hidden" name="userUpdate" value="true" />
                            </g:if>

                            <td><g:link controller="round" action="edit" id="${round.id}">${round}</g:link></td>

                            <g:if test="${round.selection}">
                              <td>${fieldValue(bean: round, field: "selection")}</td>
                            </g:if>
                            <g:else>
                              <td><g:select name="selection.id" from="${Restaurant.list()}" optionKey="id" optionValue="name" noSelection="${['null':'']}" /></td>
                            </g:else>

                            <td>
                            	<g:each in="${round.votes.sort { it.vote }.reverse()}">
                            		${it.restaurant} (${it.vote})
                            	</g:each>
                            </td>
                        
                            <td>
                              <g:if test="${round.@price != null}">
                                <g:formatNumber number="${round.price/100}" type="currency" locale="${locale}" currencyCode="EUR" />
                              </g:if>
                              <g:else>
                                <div style="float:left;">
                                  <input id="lunchPrice" name="price" type="text" value="${(params.price) ? (g.formatNumber(number:(params?.price/100), type:'currency', locale:locale, currencyCode:'EUR')) : ''}" /> <g:message code="currency" default="&euro;" />
                                  <g:if test="${loggedInUser?.company?.luncheonVoucherProfitValue > 0}">
                                    <br />
                                    <label for="luncheonVoucherUsed"><g:message code="luncheonVoucherUsed" default="Luncheon voucher used:" /></label>
                                    <input id="luncheonVoucherUsed" name="luncheonVoucherUsed" type="checkbox" /><br />
                                  </g:if>
                                </div>
                              </g:else>

                              <g:if test="${!round.selection || round.@price == null}">
                                <input style="float:right" type="submit" value="${g.message(code:'save', default:'Save')}" />
                              </g:if>
                            </td>
                        
                            <td><g:join in="${round.lunchMates()}" delimiter=", "/></td>

                            <g:if test="${!round.selection || !round.price}">
                              </form>
                            </g:if>
                        </tr>
                    </g:each>
                    </tbody>
            	</table>
            </div>
            <div class="paginateButtons">
                <g:paginate id="${params.id}" action="show" total="${roundsTotal}" />
            </div>
        </div>
    </body>
</html>
